package com.fms.trollalarm.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fms.trollalarm.R;
import com.fms.trollalarm.custom.MoveAnimation;
import com.fms.trollalarm.object.LevelType;
import com.fms.trollalarm.object.TrollType;
import com.fms.trollalarm.utils.Common;
import com.fms.trollalarm.utils.LevelSetting.Easy;
import com.fms.trollalarm.utils.LevelSetting.Hard;
import com.fms.trollalarm.utils.LevelSetting.Medium;

public class TrollButtonFragment extends AlarmFragment {

	// Layout
	private TextView tvTitle;
	private Button btSnooze;
	private RelativeLayout rlContent;
	private Button btChange;
	int numDismiss = 0;
	long speed = 500;

	public static TrollButtonFragment newInstance(int levelType) {
		TrollButtonFragment fragment = new TrollButtonFragment();
		Bundle bundle = new Bundle();
		bundle.putInt(Common.IntentExtra.TROLL_LEVEL, levelType);
		fragment.setArguments(bundle);
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_troll_button, null);
		findViewIDs(v);
		init();
		setEventView();
		return v;
	}

	private void findViewIDs(View v) {
		tvTitle = (TextView) v.findViewById(R.id.tv_title);
		btSnooze = (Button) v.findViewById(R.id.bt_snooze);
		rlContent = (RelativeLayout) v.findViewById(R.id.rl_content);
		btChange = (Button) v.findViewById(R.id.bt_change);
	}

	private void init() {
		setTrollLevel();
	}

	private void setTrollLevel() {
		int trollLevel = getTrollLevel();
		if (trollLevel == LevelType.EASY.ordinal()) {
			speed = Easy.TROLL_BUTTON_SPEED;
		} else if (trollLevel == LevelType.MEDIUM.ordinal()) {
			speed = Medium.TROLL_BUTTON_SPEED;
		} else {
			speed = Hard.TROLL_BUTTON_SPEED;
		}
	}

	private void setEventView() {
		btSnooze.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setOnSnoozeAlarm();
			}
		});

		btChange.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setOnChangeTrollType(TrollType.TROLL_BUTTON);
			}
		});

		rlContent.getViewTreeObserver().addOnGlobalLayoutListener(
				new OnGlobalLayoutListener() {

					// @SuppressLint("NewApi")
					@Override
					public void onGlobalLayout() {
						rlContent.getViewTreeObserver()
								.removeGlobalOnLayoutListener(this);
						// // Log.i("fms", "rlcontent x1 " +
						// rlContent.getLeft());
						// Log.i("fms", "rlcontent x2 " + rlContent.getRight());
						// Log.i("fms", "rlcontent y1 " + rlContent.getTop());
						// Log.i("fms", "rlcontent y2 " +
						// rlContent.getBottom());
						// // width = rlContent.getWidth();
						// // height = rlContent.getHeight();
						// // randomButton();

						// Log.i("fms", "btOk.getWidth() " + btOk.getWidth());
						// Log.i("fms", "xMax " + xMax);
						// Log.i("fms", "yMax " + yMax);
						// Log.i("fms", "xMin " + xMin);
						// Log.i("fms", "yMin " + yMin);
						// getTranslateAnimation();
						// btOk.startAnimation(getTranslateAnimation());
						// btOk.animate().x(xMax).y(yMax)
						// .setListener(animatorListener);
						// btOk.animate().x(xMin).y(yMin);
						// MoveAnimation moveBtOK = new MoveAnimation(btOk,
						// xMax,
						// yMax, xMin, yMin);
						// moveBtOK.setOnClickViewListener(new
						// OnClickViewListener() {
						//
						// @Override
						// public void onClick() {
						// seton
						// }
						// });
						// moveBtOK.start();
						// MoveAnimation moveBtCancel = new MoveAnimation(
						// btCancel, xMax, yMax, xMin, yMin);
						// moveBtCancel.start();
						createFakeDismissButton();
						for (int i = 0; i < 5; i++) {
							createCancelButton();
						}
					}
				});
		// btCancel.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// createFakeDismissButton();
		// }
		// });
	}

	private void createFakeDismissButton() {
		if (numDismiss > 10) {
			return;
		}
		ImageView view = new ImageView(getActivity());
		// button.setText("Dismiss");
		view.setBackgroundResource(R.drawable.troll);
		// RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
		// LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.d("fms", "click dismiss");
				rlContent.removeView(v);
				numDismiss--;
				if (numDismiss <= 0) {
					setOnStopAlarm();
				}
			}
		});
		rlContent.addView(view);
		MoveAnimation moveBt = new MoveAnimation(view, rlContent, getActivity());
		// moveBt.setOnClickViewListener(new OnClickViewListener() {
		//
		// @Override
		// public void onClick(View v) {
		// rlContent.removeView(v);
		// numDismiss--;
		// if (numDismiss == 0) {
		// setOnStopAlarm();
		// }
		// }
		// });
		moveBt.start(speed + (numDismiss * 50), 0);
		numDismiss++;
	}

	private void createDismissButton() {
		Button button = new Button(getActivity());
		button.setText("Dismiss");
		// RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
		// LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setOnStopAlarm();
			}
		});
		rlContent.addView(button);
		MoveAnimation moveBt = new MoveAnimation(button, rlContent,
				getActivity());
		// moveBt.setOnClickViewListener(new OnClickViewListener() {
		//
		// @Override
		// public void onClick(View v) {
		// setOnStopAlarm();
		// }
		// });
		moveBt.start(speed, 0);
	}

	private void createCancelButton() {
		ImageView view = new ImageView(getActivity());
		// button.setText("Dismiss");
		view.setBackgroundResource(R.drawable.ic_launcher);
		// RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
		// LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.d("fms", "click cancel");
				createFakeDismissButton();
			}
		});
		rlContent.addView(view);
		MoveAnimation moveBt = new MoveAnimation(view, rlContent, getActivity());
		// moveBt.setOnClickViewListener(new OnClickViewListener() {
		//
		// @Override
		// public void onClick(View v) {
		// createFakeDismissButton();
		// }
		// });
		moveBt.start(speed, 0);
	}

	// protected void randomButton() {
	// Random rnd = new Random();
	// for (int i = 0; i < 20; i++) {
	// Button button = new Button(getActivity());
	// button.setText("aaaaaaaaaa");
	// RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
	// 150, 80);
	// // params.leftMargin = rnd.nextInt(width - 100);
	// //
	// // params.topMargin = rnd.nextInt(height - 80);
	// rlContent.addView(button, params);
	// ;
	// }
	// }

}
